package com.demo.account_service.controller;

import com.demo.account_service.application.dto.AccountInDto;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.demo.account_service.application.service.AccountService;

import java.util.List;

@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("api/account")
public class AccountController {
    private final AccountService accountService;

    @PostMapping("/test")
    public ResponseEntity<?> testaccount(@RequestBody AccountInDto accountInDto,
                                         BindingResult bindingResult) {
        //request validation
        if (bindingResult.hasErrors()) {
            return ResponseEntity.badRequest().body(bindingResult.getFieldError().getDefaultMessage());
        }

        var result=accountService.checkAccount(accountInDto);
        if(result){
            return ResponseEntity.ok().body(result);
        }else{
            System.out.println(ResponseEntity.badRequest().body(result));
            return ResponseEntity.badRequest().body(result);
        }
    }
    private List<String> buildValidationError(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return bindingResult.getFieldErrors().stream()
                    .map(error -> error.getField() + ": " + error.getDefaultMessage()).toList();
        }
        return null;
    }
}
